package org.chat.client.screens;

import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Le Texte Box
 * @author AGRAM Larbi
 *@version 1.1
 */
public class LabelledTextBox extends TextBox implements FocusHandler {

	private final String labelText;
	private boolean hasUserText = false;

	/**
	 * 
	 * @param labelText
	 */
	public LabelledTextBox(String labelText) {
		this.labelText = labelText;
		setText(labelText);
		setActive(false);
		addFocusHandler(this);

	}

	/**
	 * Changer si un texte box sera activer ou non 
	 * @param isActive
	 */
	private void setActive(boolean isActive) {
		final String styleName = "inactive";
		if (isActive) {
			removeStyleName(styleName);
		} else {
			addStyleName(styleName);
		}
	}

	/**
	 * 
	 * @param sender
	 */
	public void onLostFocus(FocusEvent sender) {
		String text = getText();
		hasUserText = (text.length() > 0);    
		if (!hasUserText) {
			setText(labelText);
		}
		setActive(hasUserText);
	}

	/**
	 * L'action qand on click hors du texte box
	 */
	public void onFocus(FocusEvent event) {
		// TODO Auto-generated method stub
		if (!hasUserText) {
			setText("");
		}
		setActive(true);
	}
}
